<template>
  <div class="product">
    <div class="product_img">
      <span class="back">
        <img src="../assets/img/product/3.webp" alt="" @click="backButton" />
      </span>
      <span class="share">
        <img src="../assets/img/product/2.webp" alt="" />
      </span>
      <van-image fit="scale-down" :src="obj.imgurl" />
    </div>
    <p class="product_title">{{ obj.title }}</p>
    <p class="product_detail">{{ obj.littleTitle }}</p>
    <p class="product_price">¥{{ obj.price }}</p>
    <div class="footer">
      <div class="serve">
        <img src="../assets/img/product/6.webp" alt="" />
        <p>客服</p>
      </div>
      <div class="cart" @click="gocart">
        <img src="../assets/img/product/7.webp" alt="" />
        <p>购物车</p>
      </div>
      <p class="button">
        <span class="addcart" @click="addcart">加入购物车</span>
        <span class="pay">立即购买</span>
      </p>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, watch } from "vue";
import { Notify } from "vant";
import http from "../utils/api";
import router from "@/router/index.js";
import { useRoute } from "vue-router";
export default {
  setup() {
    const route = useRoute();
    const id = route.path.split("/")[2];
    // console.log(id);
    const obj = ref({});
    const addcart = function () {
      let arr1 = localStorage.getItem("_id");
      if (arr1) {
        arr1 = localStorage.getItem("_id").split(",");
        if (arr1.indexOf(id) + 1) {
          Notify({ type: "danger", message: "该商品已存在" });
        } else {
          arr1.push(id);
          localStorage.setItem("_id", arr1);
        }
      } else {
        localStorage.setItem("_id", id);
        Notify({ type: "success", message: "加入购物车成功" });
      }
      // localStorage.setItem({  });
    };
    onMounted(() => {
      http.getIdProductApi({ _id: id }).then((res) => {
        console.log(res.data);
        obj.value = res.data.data[0];
        console.log(obj.value);
      });
    });
    const backButton = () => {
      router.go(-1);
    };
    const gocart = () => {
      router.push("/cart");
    };
    return {
      obj,
      backButton,
      watch,
      addcart,
      gocart,
    };
  },
};
</script>
<style lang="less" scoped>
.product {
  width: 100%;
  .clearfix::after {
    content: ".";
    display: block;
    height: 0;
    overflow: hidden;
    visibility: hidden;
    clear: both;
  }
  .product_img {
    width: 100%;
    height: 290px;
    position: relative;
    background-color: rgb(184, 184, 184);
    .back {
      position: absolute;
      top: 10px;
      left: 10px;
      z-index: 1000;
      img {
        width: 13px;
        height: 20px;
      }
    }
    .share {
      position: absolute;
      right: 10px;
      top: 10px;
      z-index: 1000;
      img {
        width: 21px;
        height: 21px;
      }
    }
    .van-image {
      width: 100%;
      height: 290px;
    }
  }
  .product_title {
    font-size: 17px;

    line-height: 24px;
    padding: 34px 24px;
  }
  .product_detail {
    font-size: 14px;
    line-height: 24px;
    padding: 0 24px;
    color: grey;
  }
  .product_price {
    font-size: 22px;
    padding: 10px 24px;
    color: red;
  }
  .footer {
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;
    div {
      color: grey;
      font-size: 13px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 70px;
      height: 50px;
    }
    .serve {
      img {
        width: 17px;
        height: 18px;
      }
    }
    .cart {
      img {
        width: 19px;
        height: 17px;
      }
    }
    .button {
      width: 220px;
      height: 50px;
      span {
        display: block;
        float: left;
        width: 110px;
        height: 50px;
        font-size: 16px;
        text-align: center;
        line-height: 50px;
        color: #fff;
      }
      .addcart {
        background-color: #ff9a4c;
      }
      .pay {
        background-color: #fe374e;
      }
    }
  }
}
</style>
